package com.tbi.graphcolouring.algorithmconfiguration.stopper;

import java.util.List;

import com.tbi.algorithm.configuration.IAlgorithmStopper;
import com.tbi.graphcolouring.PopulationEntity;

public class NoImprovementStopper implements IAlgorithmStopper {
	private int maxGenerationsWithoutImprovement;
	
	private int bestRating = Integer.MAX_VALUE;
	private int bestRatingsGeneration;
	
	public NoImprovementStopper(int maxGenerationsWithoutImprovement) {
		this.maxGenerationsWithoutImprovement = 
				maxGenerationsWithoutImprovement;
	}

	@Override
	public boolean stopCondition(List<Object> population, int generation) {
		if(generation % 10 == 0){
			System.out.println(generation+"/?");
		}
		for(Object obj : population){
			PopulationEntity entity = (PopulationEntity) obj;
			if(entity.rating < bestRating){
				bestRating = entity.rating;
				bestRatingsGeneration = generation;
				return false;
			}
		}
		boolean result = generation - bestRatingsGeneration > maxGenerationsWithoutImprovement;
		
		if(result){
			bestRating = Integer.MAX_VALUE;
			bestRatingsGeneration = 0;
		}
		
		return result;
	}

}
